package me.khrystal.rxnews.widget;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;

import com.socks.library.KLog;

/**
 * @FileName: me.khrystal.rxnews.widget.AutoLoadMoreRecyclerView.java
 * @Fuction: AutoLoadMore
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-03-08 11:43
 * @UpdateUser:
 * @UpdateDate:
 */
public class AutoLoadMoreRecyclerView extends RecyclerView{

    private int[] mVisiblePositions;

    //state
    public static final int STATE_MORE_LOADING = 1;
//    只有在此状态下才能加载
    public static final int STATE_MORE_LOADED = 2;
//    全部加载完毕 不能再进行加载
    public static final int STATE_ALL_LOADED = 3;

    private int mCurrentState = STATE_MORE_LOADED;
    private OnLoadMoreListener mLoadMoreListener;


    public boolean isMoreLoading(){
        return mCurrentState == STATE_MORE_LOADING;
    }

    public boolean isAllLoaded(){
        return mCurrentState == STATE_ALL_LOADED;
    }

    public AutoLoadMoreRecyclerView(Context context) {
        super(context);
        init();
    }

    public AutoLoadMoreRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AutoLoadMoreRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init(){
        super.addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (mCurrentState == STATE_MORE_LOADED
                        && RecyclerView.SCROLL_STATE_IDLE == newState
                        && calculateRecyclerViewLastPosition()==getAdapter().getItemCount()-1
                        && mLoadMoreListener!=null){
                    KLog.e("Loading More");
                    mLoadMoreListener.loadMore();
                    mCurrentState = STATE_MORE_LOADING;
                }
            }
        });
    }

    public void notifyMoreLoaded(){
        mCurrentState = STATE_MORE_LOADED;
    }

    public void notifyAllLoaded(){
        mCurrentState = STATE_ALL_LOADED;
    }

    public void setOnLoadMoreListener(OnLoadMoreListener loadMoreListener){
        mLoadMoreListener = loadMoreListener;
    }

    private int calculateRecyclerViewLastPosition(){
        if (getLayoutManager() instanceof StaggeredGridLayoutManager) {
            if (mVisiblePositions == null) {
                mVisiblePositions = new int[((StaggeredGridLayoutManager) getLayoutManager())
                        .getSpanCount()];
            }
            ((StaggeredGridLayoutManager) getLayoutManager())
                    .findLastCompletelyVisibleItemPositions(mVisiblePositions);
            int max = -1;
            for (int pos : mVisiblePositions) {
                max = Math.max(max, pos);
            }
            return max;
            // return mVisiblePositions[0];
        } else if (getLayoutManager() instanceof GridLayoutManager) {
            return ((GridLayoutManager) getLayoutManager()).findLastCompletelyVisibleItemPosition();
        } else {
            return ((LinearLayoutManager) getLayoutManager())
                    .findLastCompletelyVisibleItemPosition();
        }
    }

    public AutoLoadMoreRecyclerView setAutoLayoutManager(LayoutManager layoutManager){
        super.setLayoutManager(layoutManager);
        return this;
    }

    public AutoLoadMoreRecyclerView addAutoItemDecoration(ItemDecoration decoration){
        super.addItemDecoration(decoration);
        return this;
    }

    public AutoLoadMoreRecyclerView setAutoItemAnimator(ItemAnimator anim){
        super.setItemAnimator(anim);
        return this;
    }

    public AutoLoadMoreRecyclerView setAutoAdapter(Adapter adapter){
        super.setAdapter(adapter);
        return this;
    }

    public AutoLoadMoreRecyclerView setAutoHasFixedSize(boolean fixed){
        super.setHasFixedSize(fixed);
        return this;
    }

    public AutoLoadMoreRecyclerView setAutoItemAnimatorDuration(int duration) {
        super.getItemAnimator().setAddDuration(duration);
        super.getItemAnimator().setMoveDuration(duration);
        super.getItemAnimator().setChangeDuration(duration);
        super.getItemAnimator().setRemoveDuration(duration);
        return this;
    }




    public interface OnLoadMoreListener {
        void loadMore();
    }
}
